﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PetaPoco;
using Happy.Query.PetaPoco;
using Happy.Cms.Query.Articles;

namespace Happy.Cms.Query.PetaPoco.Articles
{
    public sealed class ArticleQuerySource : QuerySource<ArticleView>, IArticleQuerySource
    {
        public ArticleQuerySource()
            : base("CmsUnitOfWork", "Articles")
        {
        }

        protected override ArticleView SingleOrDefault(Database db, object id, bool includeRelation = true)
        {
            var model = base.SingleOrDefault(db, id, includeRelation);

            if (includeRelation)
            {
                model.AssignedTags = db.Fetch<Guid>("SELECT TagId FROM AssignedTags WHERE ArticleId = @0", model.Id);
            }

            return model;
        }

        protected override string[] DefaultOrderClauses
        {
            get
            {
                return new string[] { "([Order])", "PublishDate" };
            }
        }
    }
}
